package com.luke.web.controller.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.luke.base.domain.RetrieveAlarm;
import com.luke.base.vo.Status;
import com.luke.base.vo.Vf;
import com.luke.common.core.domain.AjaxResult;
import com.luke.base.domain.vo.Location;
import com.luke.framework.web.domain.server.Sys;

import java.util.ArrayList;
import java.util.List;

public class LukeApiUtil {
    public static final String URL="Http://position.gnets.cn:8090/gpstar/";
    public static final String CUSTID="PRY9I2xU";

    //1.获取最后一次定位位置
    public static AjaxResult location(String carId) throws HttpStatusException {
        String url=URL+"location?cust_id="+CUSTID+"&car_id="+carId;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        if(("OK").equals(obj.get("ret"))){
            JSONArray jsonArray= (JSONArray) obj.get("location");
            Location location= JSON.toJavaObject((JSON) jsonArray.get(0),Location.class);
            return AjaxResult.success(location);
        }
        return AjaxResult.error(obj.get("code").toString());
    }
    //2.获取轨迹信息
    public static AjaxResult track(String carId,String startTime ,String endTime) throws HttpStatusException {
        String url=URL+"track?cust_id="+CUSTID+"&car_id="+carId+"&start_time="+startTime+"&end_time="+endTime;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        List<Location> locationList=new ArrayList<>();
        if(("OK").equals(obj.get("ret"))){
            locationList=JSONArray.parseArray(obj.get("track").toString(),Location.class);
        }else {
            Integer code=(Integer)obj.get("code");
            if(code!=null && code != 20013){
                return AjaxResult.error(code+"");
            }
        }
        return AjaxResult.success(locationList);
    }
    //3.查询指定范围内的设备
    public static String around(String maxLon,String minLon ,String maxLat ,String minLat) throws HttpStatusException {
        String url=URL+"around?cust_id="+CUSTID+"&maxlon=117012345&minlon=117012245&maxlat=36012345&minlat=36012344";
        return HttpUtil.doGet(url,"GET");
    }

    //4.查询设备状态
    public static AjaxResult status(String carId) throws HttpStatusException {
        String url=URL+"status?cust_id="+CUSTID+"&car_id="+carId;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        if(("OK").equals(obj.get("ret"))){
            Status status= JSON.toJavaObject((JSONObject) obj.get("status"),Status.class);
            return AjaxResult.success(status);
        }
        return AjaxResult.error(obj.get("code").toString());
    }

    public static JSONObject LocationSummary() throws HttpStatusException{
        String url=URL+"LocationSummary?cust_id="+CUSTID;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        return obj;
    }

    public static List<Location> LocationPaging() throws HttpStatusException{
        List<Location> list=new ArrayList<>();
        JSONObject result=LocationSummary();
        if(("OK").equals(result.get("ret"))){
            JSONObject summary= (JSONObject) result.get("summary");
             int total= (int) summary.get("total");
             int max= (int) summary.get("max");
             int n=total/max;
            for (int i = 0; i <=n ; i++) {
                String url=URL+"LocationPaging?cust_id="+CUSTID+"&offset="+(i*max)+"&size="+max;
                String r=HttpUtil.doGet(url,"GET");
                JSONObject obj=JSONObject.parseObject(r);
                if(("OK").equals(obj.get("ret"))){
                    List<Location> aa=JSONArray.parseArray(obj.get("paging").toString(),Location.class);
                    list.addAll(aa);
                }
            }
        }
        return list;
    }

    //车辆远程控制
    public static AjaxResult control(String carId,int io,String flag) throws HttpStatusException{
        String url=URL+"control?cust_id="+CUSTID+"&car_id="+carId+"&io="+io+"&flag="+flag;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        String message="";
        if(("OK").equals(obj.get("ret"))){
            if(io==2){
                if("1".equals(flag)){
                    message="钥匙门开启成功";
                }else {
                    message="钥匙门关闭成功";
                }
            }else if (io == 3){
                if("1".equals(flag)){
                    message="车辆电机轮已锁定";
                }else {
                    message="车辆电机轮已解锁";
                }
            }else if (io == 1){
                if("1".equals(flag)){
                    message="语音喇叭已开启";
                }else {
                    message="语音喇叭已关闭";
                }
            }else if (io == 6){
                if("1".equals(flag)){
                    message="车门锁已打开";
                }else {
                    message="车门锁已关闭";
                }
            }
            return AjaxResult.success(message);
        }else {
            if(io==2){
                if("1".equals(flag)){
                    message="钥匙门打开失败";
                }else {
                    message="钥匙门关闭失败";
                }
            }else if (io == 3){
                if("1".equals(flag)){
                    message="远程锁车失败";
                }else {
                    message="远程解锁失败";
                }
            }else if (io == 1){
                if("1".equals(flag)){
                    message="语音喇叭开启失败";
                }else {
                    message="语音喇叭关闭失败";
                }
            }else if (io == 6){
                if("1".equals(flag)){
                    message="车门锁开启失败";
                }else {
                    message="车门锁关闭失败";
                }
            }
        }
        return AjaxResult.error(message);
    }

    //设备报警信息
    public static List<RetrieveAlarm> retrieveAlarm() throws HttpStatusException{
        String url=URL+"retrieveAlarm?cust_id="+CUSTID;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        List<RetrieveAlarm> alarmList=new ArrayList<>();
        if(("OK").equals(obj.get("ret"))){
            alarmList=JSONArray.parseArray(obj.get("retrieveAlarm").toString(),RetrieveAlarm.class);
        }
        return alarmList;
    }

    //设备报警信息
    public static AjaxResult through2(String carId) throws HttpStatusException{
        String url=URL+"through2?cust_id="+CUSTID+"&car_id="+carId+"&cmd=RESET#";
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        if(("RESET OK").equals(obj.get("ret"))){
            return AjaxResult.success("设备重启成功");
        }
        return AjaxResult.error("设备重启失败");
    }

    //查询设备实时位置
    public static JSONObject devgps(String carId) throws HttpStatusException {
        String url=URL+"devgps?cust_id="+CUSTID+"&car_id="+carId;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        return obj;
    }
    //查询电子围栏
    public static AjaxResult queryvf(String carId) throws HttpStatusException {
        String url=URL+"queryvf?cust_id="+CUSTID+"&car_id="+carId;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        if(("OK").equals(obj.get("ret"))){
            Vf vf= JSON.toJavaObject((JSONObject) obj.get("vf"),Vf.class);
            return AjaxResult.success(vf);
        }
        return AjaxResult.error("获取电子围栏失败");
    }

    //设置电子围栏
    public static AjaxResult openvf(String carId) throws HttpStatusException {
        //首先查询设备实时位置
        JSONObject dev = devgps(carId);
        if(!("OK").equals(dev.get("ret"))){
            return AjaxResult.error("开启电子围栏失败");
        }
        if(("OK").equals(dev.get("ret"))){
            JSONArray jsonArray= (JSONArray) dev.get("location");
            JSONObject location = (JSONObject) jsonArray.get(0);
            String lon=location.getString("lon_r");
            String lat=location.getString("lat_r");
            String url=URL+"openvf?cust_id="+CUSTID+"&car_id="+carId +"&lon="+lon +"&lat="+lat;
            String result=HttpUtil.doGet(url,"GET");
            JSONObject obj=JSONObject.parseObject(result);
            if(("OK").equals(obj.get("ret"))){
                return AjaxResult.success("开启电子围栏成功");
            }
        }
        return AjaxResult.error("开启电子围栏失败");
    }
    //关闭电子围栏
    public static AjaxResult closevf(String carId) throws HttpStatusException {
        String url=URL+"closevf?cust_id="+CUSTID+"&car_id="+carId;
        String result=HttpUtil.doGet(url,"GET");
        JSONObject obj=JSONObject.parseObject(result);
        if(("OK").equals(obj.get("ret"))){
            return AjaxResult.success("关闭电子围栏成功");
        }
        return AjaxResult.error("关闭电子围栏失败");
    }


    public static void main(String[] args) throws HttpStatusException {
        //System.err.println(through2("621002"));
        System.err.println(queryvf("621000"));
    }

}
